package com.bank;

import android.graphics.Point;
import android.os.RemoteException;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;

import com.entity.SMSCode;
import com.entity.User;
import com.show.api.util.StringUtils;
import com.utils.ChatClient;
import com.utils.CodeUtils;
import com.utils.Constants;
import com.utils.FileHelper;
import com.utils.LogToFile;
import com.utils.UIUtils;
import com.utils.Util;


import static com.utils.ChatClient.C_updateBalance_W;
import static com.utils.ChatClient.ConfirmOrder;
import static com.utils.ChatClient.addWorkTime;
import static com.utils.ChatClient.getSerialNumber;
import static com.utils.ChatClient.getmap;
import static com.utils.ChatClient.list;
import static com.utils.ChatClient.mapDelete;
import static com.utils.ChatClient.requestSMSCode;
import static com.utils.ChatClient.user;

import org.junit.Test;
import org.junit.runner.RunWith;


import java.text.DecimalFormat;
import java.util.concurrent.ConcurrentHashMap;

import static android.os.SystemClock.sleep;
import static com.utils.ChatClient.userInfo;

/**
 * 项目名称：yzf_android_test
 * 类描述：湖北农信
 * 创建人：Administrator
 * 创建时间：2021/1/21 9:51
 * 修改人：Administrator
 * 修改时间：2021/1/21 9:51
 * 修改备注：
 */
@RunWith(AndroidJUnit4.class)
public class HBXHbank extends Bank {

    // 2016分辨率 手势密码坐标顺序为：123456789
    private static int[][] xy_gesture_2016 = new int[][]{
            {240, 940}, {535, 940}, {840, 940},
            {240, 1245}, {535, 1245}, {840, 1245},
            {240, 1560}, {535, 1560}, {840, 1560}
    };

    private static int[][] xy_123 = new int[][]{
            {300, 1410}, {540, 1410}, {790, 1410},
            {300, 1580}, {540, 1580}, {790, 1580},
            {300, 1750}, {540, 1750}, {790, 1750},
            {540, 1925}, {780, 1925}};


    static {
        BANK_FLAG = "HBNX";
        BANK_NAME = "湖北农信";
        carrier = android.os.Build.MODEL;//机型
        smsCode = "95599";
        context = InstrumentationRegistry.getTargetContext();
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
        ratio = screenWidth / 720f;
        ratioH = screenHeight / 1080f;
        Constants.CODE = "";
    }

    boolean isChangeBill = true;//是否本地缓存余额

    @Test
    public void demo() throws UiObjectNotFoundException {
        carrier = android.os.Build.MODEL;
        LogToFile.init(InstrumentationRegistry.getTargetContext(), Constants.PROT);
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        showMsg("执行脚本...");
        Constants.tryingToReadAccountInformation(mDevice, BANK_NAME);
        Constants.scriptToStart(mDevice,BANK_NAME);
        Constants.getsTheModelAndVersionNumberAndVersionName(context, BANK_NAME);
        //动态注册短信广播
        CodeUtils.initSMSReceiver(context);
        Constants.CODE = "";
        //检查usb连接状态
        while (true) {
            if (Constants.STATE_USB) {
                sleep(ChatClient.overtime_8);
                homeTransfer();
                break;
            }
        }
    }

    //检索转账：进入转账
    public void homeTransfer() throws UiObjectNotFoundException {
        showMsg("进入主页");
        closeRechargeDialog();
        //连接服务端
        ChatClient.start();
        while (true) {
            UiObject mTV_transfer = CodeUtils.getUiObject_textOrId("转账", "cn.com.nxy.mbank.hb:id/icon_name");
            if (mTV_transfer != null && mTV_transfer.exists()) {
                showMsg("点击主页转账");
                mTV_transfer.click();
                break;
            }
        }
        Login();
        while (true) {
            UiObject mTV_title = CodeUtils.getUiObject_textOrId("转账汇款", "cn.com.nxy.mbank.hb:id/tvTopTextTitle");
            if (mTV_title.exists()) {
                ChatClient.loginS();
                UiObject mTV = CodeUtils.getUiObject_textOrClass("账号转账", "android.view.View");
                if (mTV.exists()) {
                    showMsg("点击账号转账...");
                    mTV.click();
                    sleep(ChatClient.overtime_3);
                    break;
                }
            }
        }
        setTimerun();
    }

    private void Login() {
        //获取账号信息
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_60, "登陆超时", Constants.isReport);
            if (isOutTime) {
                return;
            }
            //请求登录信息
            if (userInfo.equals("")) {
                ChatClient.msg("<YZF>{'header':'" + ChatClient.M_GET_BANK_LOGIN_INFO_P + "'}</YZF>");
                sleep(5000);
            } else {
                user = new User();
                String[] res = userInfo.split(" ");
                user.setUserName(res[0]);
                user.setPwd(res[1]);
                user.setBankpwd(res[2]);
                user.setUseType(res[4]);
                user.setBankId(res[5]);
                break;
            }
        }
        showMsg("手势登录");
        while (true) {
            UiObject mTV_user = mDevice.findObject(new UiSelector().resourceId("cn.com.nxy.mbank.hb:id/tv_user_num"));
            if (mTV_user.exists()) {
                showMsg("进入登陆页面...");
                gestureLogin(user.getPwd());//手势登录
                sleep(ChatClient.overtime_2);
                break;
            }
        }
    }


    private void gestureLogin(String password) {
        showMsg("手势登录");
        char[] pwd = password.toCharArray();//char数组
        showMsg("password======"+password);
        Point[] pp = new Point[pwd.length];
        for (int i = 0; i < pwd.length; i++) {
            Point point = new Point();
            int p = Integer.valueOf(pwd[i] + "") - 1;
            point.set(xy_gesture_2016[p][0], xy_gesture_2016[p][1]);
            showMsg("pwd======" + i);
            pp[i] = point;
        }
        // 唤醒屏幕
        try {
            mDevice.wakeUp();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        mDevice.swipe(pp, 10);
    }


    /**
     * 不断进入跨行汇款，等待订单
     *
     * @throws UiObjectNotFoundException
     */
    public void setTimerun() throws UiObjectNotFoundException {
        while (true) {
            sleep(ChatClient.overtime_2);
            if (ChatClient.existOrder()) {
                if (ConfirmOrder) {
                    //查账
                    isChangeBill = false;
                    showMsg("有订单，准备进入查账");
                    getBalanceBack();
                    auditOrder();
                } else {
                    //开始转账
                    isChangeBill = true;
                    showMsg("有订单，准备进入转账");
                    getBalanceBack();
                    transfer();
                }
            } else {
                isChangeBill = true;
                showMsg("没有订单,等待订单");
                /************向T端请求订单*****************/
                getBalanceBack();
                refresh();
            }
        }
    }

    /**
     * 余额上报
     *
     * @return
     * @throws UiObjectNotFoundException
     */
    public String getBalanceBack() throws UiObjectNotFoundException {
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_30, "余额超时...", Constants.notReport);
            if (isChangeBill && isOutTime) {
                break;
            }
            UiObject mV = CodeUtils.getUiObject_textOrClass("可用余额", "android.view.View");
            if (mV.exists()) {
                UiObject mV_money = mV.getFromParent(new UiSelector().className("android.view.View").index(1));
                if (mV_money.exists()) {
                    String moneyText = mV_money.getText();
                    if (!StringUtils.isEmpty(moneyText)) {
                        sumMoney = Util.replaceMoney(moneyText);
                        C_updateBalance_W(Double.parseDouble(sumMoney));
                        if (isChangeBill) {
                            showMsg("本地保存余额");
                            String sumMoneyFile = user.getUserName() + BANK_FLAG + "_sumMoney.txt";
                            FileHelper.saveMoneySDFile(false, sumMoney, sumMoneyFile);
                        }
                        break;
                    }
                }
            }
        }
        return sumMoney;
    }

    /**
     * 来回刷新
     */
    private void refresh() {
        if (!ChatClient.waitExistOrder(ChatClient.overtime_60)) {
            showMsg("返回转账汇款页...");
            mDevice.pressBack();
            sleep(ChatClient.overtime_2);
            while (true) {
                UiObject mTV_title = CodeUtils.getUiObject_textOrId("转账汇款", "cn.com.nxy.mbank.hb:id/tvTopTextTitle");
                if (mTV_title.exists()) {
                    UiObject mTV = CodeUtils.getUiObject_textOrClass("账号转账", "android.view.View");
                    if (mTV.exists()) {
                        showMsg("点击账号转账2...");
//                        mTV.click();
                        mDevice.click(270,360);
                        mDevice.click(270,360);
                        sleep(ChatClient.overtime_3);
                        break;
                    }
                }
            }
        }
    }

    /**
     * 转账
     */
    private void transfer() throws UiObjectNotFoundException {
        if (Double.parseDouble(sumMoney) < Double.parseDouble(list.get(0).getMoney())) {
            String mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT +
                    "','phoneModel':'湖北农信银行','info':'账户余额不足'";
            String json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':1,'type':1";
            reportResults(mobileD, json);
            refresh();
            sleep(ChatClient.overtime_3);
            return;
        }
        while (true) {
            UiObject mET_name = CodeUtils.getUiObject_textOrClass("请输入收款人户名", "android.widget.EditText");
            if (mET_name.exists()) {
                showMsg("输入用户名");
                mET_name.setText(list.get(0).getUserName());
                sleep(ChatClient.overtime_2);
                break;
            }
        }
        while (true) {
            UiObject mET_bank_num = CodeUtils.getUiObject_textOrClass("请输入收款账号", "android.widget.EditText");
            if (mET_bank_num.exists()) {
                showMsg("输入账号");
                mET_bank_num.click();
                sleep(ChatClient.overtime_1);
                mDevice.click(80, 1930);
                sleep(ChatClient.overtime_1);
                clickNum(list.get(0).getBankNo());
                sleep(ChatClient.overtime_1);
                break;
            }
        }
        while (true) {
            UiObject mET_bank_num = CodeUtils.getUiObject_textOrId("请输入转账金额", "P030100Money");
            if (mET_bank_num.exists()) {
                showMsg("输入金额");
                mET_bank_num.setText(list.get(0).getMoney());
                sleep(ChatClient.overtime_2);
                break;
            }
        }
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_6, "银行识别成功", Constants.notReport);
            if (isOutTime) {
                break;
            }
            UiObject mET_bank_name = CodeUtils.getUiObject_textOrClass("请选择收款行名", "android.widget.EditText");
            if (mET_bank_name.exists()) {
                showMsg("选择银行");
                mET_bank_name.click();
                sleep(ChatClient.overtime_2);
                boolean b = selectBankName();
                if (b) {
                    break;
                } else {
                    return;
                }
            }
        }
        sleep(ChatClient.overtime_1);
        while (true) {
            UiObject mBT_next = CodeUtils.getUiObject_textOrClass("下一步", "android.widget.Button");
            if (mBT_next.exists()) {
                showMsg("点击下一步");
                mBT_next.click();
                sleep(ChatClient.overtime_3);
                break;
            }
        }
        closeRechargeDialog();
        smsOrPaw();
        getOrderInfo();
    }

    private boolean selectBankName() throws UiObjectNotFoundException {
        String bankName = list.get(0).getBankName();
        while (true) {
            UiObject mET_bank_name = CodeUtils.getUiObject_textOrClass("请输入银行名、完整拼音、或首字母查询", "android.view.View");
            if (mET_bank_name.exists()) {
                mET_bank_name.setText(bankName);
                sleep(ChatClient.overtime_2);
                break;
            }
        }

        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_10, "无银行条目", Constants.notReport);
            if (isOutTime) {
                String mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT +
                        "','phoneModel':'湖北农信银行','info':'银行名错误'";
                String json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':1,'type':1";
                reportResults(mobileD, json);
                mDevice.pressBack();
                sleep(ChatClient.overtime_1);
                mDevice.pressBack();
                sleep(ChatClient.overtime_2);
                backTransferPage();
                return false;
            }
            UiObject mLV = mDevice.findObject(new UiSelector().className("android.widget.ListView").index(0));
            if (mLV.exists()) {
                UiObject mV_item = mLV.getChild(new UiSelector().text(bankName).className("android.view.View"));
                if (mV_item.exists()) {
                    showMsg("点击银行：" + bankName);
                    mV_item.click();
                    sleep(ChatClient.overtime_3);
                    return true;
                }
            }
        }
    }

    /**
     * 短信密码输入
     */
    private void smsOrPaw() throws UiObjectNotFoundException {
        addWorkTime(ChatClient.overtime_90);
        while (true) {
            UiObject mBT = mDevice.findObject(new UiSelector().resourceId("resend-btn"));
            if (mBT.exists()) {
                showMsg("点击获取短信");
                mBT.click();
                break;
            }
        }
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_60, "短信验证码超时错误", Constants.isReport);
            if (isOutTime) {
                while (true) {
                    UiObject mBT = mDevice.findObject(new UiSelector().resourceId("resend-btn"));
                    if (mBT.exists()) {
                        showMsg("点击获取短信");
                        mBT.click();
                        break;
                    }
                }
            }
            addWorkTime(ChatClient.overtime_60);
            if (user.getUseType().equals("1")) {
                showLog("处理转账短信---真机");
                if (TextUtils.isEmpty(Constants.CODE)) {
                    sleep(ChatClient.overtime_1);
                } else {
                    showLog("返回短信验证码为：" + Constants.CODE);
                    break;
                }
            } else {
                showLog("处理转账短信---无SIM卡");
                if (setNotSIM_SMS()) {
                    break;
                } else {//未收到短信
                    return;
                }
            }
        }
        sleep(ChatClient.overtime_1);
        while (true) {
            UiObject mET = CodeUtils.getUiObject_textOrId("请输入短信验证码", "AuthBox_SMS");
            if (mET.exists()) {
                showMsg("赋值验证码...");
                mET.setText(Constants.CODE);
                Constants.CODE = null;
                sleep(ChatClient.overtime_1);
                break;
            }
        }

        while (true) {
            UiObject mET = CodeUtils.getUiObject_textOrId("请输入手机银行交易密码", "AuthBox_DRAW_PWD");
            if (mET.exists()) {
                showMsg("等待交易密码输入...");
                mET.click();
                sleep(ChatClient.overtime_30);
                break;
            }
        }
        //        while (true) {
        //            UiObject mBT = CodeUtils.getUiObject_textOrClass("确认提交", "android.widget.Button");
        //            if (mBT.exists()) {
        //                showMsg("确认提交...");
        //                mBT.click();
        //                break;
        //            }
        //        }
        //        ChatClient.begintime = System.currentTimeMillis();//开始时间
        //        while (true) {
        //            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_5, "验证错误超时...", Constants.notReport);
        //            if (isOutTime) {
        //                break;
        //            }
        //            UiObject mTV = CodeUtils.getUiObject_textOrId("输入短信验证码错误", "cn.com.nxy.mbank.hb:id/tv_msg");
        //            if (!mTV.exists()) {
        //                mTV = CodeUtils.getUiObject_textOrId("交易密码错误", "cn.com.nxy.mbank.hb:id/tv_msg");
        //            }
        //            if (mTV.exists()) {
        //                showMsg("验证错误...");
        //                closeRechargeDialog();
        //                sleep(ChatClient.overtime_1);
        //                smsOrPaw();
        //            }
        //        }
    }

    private boolean setNotSIM_SMS() {
        sleep(ChatClient.overtime_10);
        SMSCode smsCode = requestSMSCode(user.getUserName(), user.getBankId());
        if (StringUtils.isEmpty(smsCode.getSmscode())) {
            ChatClient.timer(LogToFile.getMethodName(), "获取不到验证码");
            return false;
        } else {
            Constants.CODE = smsCode.getSmscode();
            return true;
        }
    }

    /**
     * 结果页处理
     *
     * @throws UiObjectNotFoundException
     */
    public void getOrderInfo() throws UiObjectNotFoundException {
        ChatClient.msg("<YZF>{'header':'" + ChatClient.M_TRANSFERFINISH_P + "'}</YZF>");
        String balance, type;
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_30, "转账状态未知", Constants.notReport);
            if (isOutTime) {
                balance = sumMoney;
                type = "转账状态未知，请人工处理";
                break;
            }
            UiObject mV_resultInfo = CodeUtils.getUiObject_textOrClass("交易已受理", "android.view.View");
            //增加同行显示  转账成功
            UiObject mV_resultInfoTH = CodeUtils.getUiObject_textOrClass("转账成功", "android.view.View");
            if (mV_resultInfo.exists() || mV_resultInfoTH.exists()) {
                showMsg("转账成功");
                DecimalFormat formater = new DecimalFormat("#0.##");
                balance = formater.format(Double.parseDouble(sumMoney) - Double.parseDouble(list.get(0).getMoney()));
                showLog("保存余额...");
                String sumMoneyFile = user.getUserName() + BANK_FLAG + "_sumMoney.txt";
                FileHelper.saveMoneySDFile(false, balance, sumMoneyFile);
                type = "成功";
                break;
            }
        }
        String mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'兴业银行','info':'" + type + "'";
        String json = "'order':'" + list.get(0).getOrder() + "','balance':'" + balance + "','state':" + Constants.getStr(type)
                + ",'type':1";
        reportResults(mobileD, json);
        sleep(ChatClient.overtime_2);
        while (true) {
            UiObject mBT = CodeUtils.getUiObject_textOrClass("再转一笔", "android.widget.Button");
            if (mBT.exists()) {
                showMsg("再转一笔...");
                mBT.click();
                sleep(ChatClient.overtime_2);
                mDevice.click(280,1910);
                mDevice.drag(100, 900, 100, 1500, 30);
                break;
            }
        }
    }

    private void reportResults(String mobileD, String json) {
        ChatClient.msg("<YZF>{'header':'" + ChatClient.M_SEND_TRADE_RESULT_P + "'," + json + "," + mobileD + "}</YZF>");
        mapDelete(list.get(0).getOrder());
        getmap();
    }

    private void clickNum(String num) {
        char[] nums = "1234567890.".toCharArray();//char数组
        char[] bankNum = num.toCharArray();//char数组
        for (int j = 0; j < bankNum.length; j++) {
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] == bankNum[j]) {
                    showLog("点击---" + j);
                    mDevice.click(xy_123[i][0], xy_123[i][1]);
                    sleep(1000);
                    break;
                }
            }
        }
        showMsg("收回键盘");
        mDevice.click(990, 1250);
    }

    /**
     * 查账
     */
    private void auditOrder() throws UiObjectNotFoundException {
        showMsg("返回转账汇款页面...");
        mDevice.pressBack();
        sleep(ChatClient.overtime_2);
        while (true) {
            UiObject mV = CodeUtils.getUiObject_textOrClass("转账查询", "android.view.View");
            if (mV.exists()) {
                showMsg("进入账单查询页");
                mV.click();
                sleep(ChatClient.overtime_5);
                break;
            }
        }
        while (true) {
            UiObject mTV_title = CodeUtils.getUiObject_textOrId("转账查询", "cn.com.nxy.mbank.hb:id/tvTopTextTitle");
            if (mTV_title.exists()) {
                showMsg("进入查询页面");
                break;
            }
        }

        while (true) {
            UiObject mV = CodeUtils.getUiObject_textOrClass("近一周", "android.view.View");
            if (mV.exists()) {
                showMsg("选择一周筛选");
                mV.click();
                sleep(ChatClient.overtime_1);
                UiObject mBT = CodeUtils.getUiObject_textOrClass("查询", "android.widget.Button");
                if (mBT.exists()) {
                    showMsg("点击查询...");
                    mBT.click();
                    sleep(ChatClient.overtime_5);
                }
                break;
            }
        }

        while (true) {
            UiObject mV = CodeUtils.getUiObject_textOrClass("没有查询到数据", "android.view.View");
            if (mV.exists()) {
                String json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':1,'type':1";
                String mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "'," +
                        "'phoneModel':'湖北农信银行','info':'查询超时'";
                failureToReport(json, mobileD);
                backTransferPage();
                return;
            }
            UiObject mList = mDevice.findObject(new UiSelector().resourceId("list").index(3));
            if (mList.exists()) {
                UiObject mV1 = mList.getChild(new UiSelector().index(0).className("android.view.View"));
                if (mV1.exists()) {
                    UiObject mV_item = mV1.getChild(new UiSelector().index(0).className("android.view.View"));
                    if (mV_item.exists()) {
                        showMsg("点击第一条目...");
                        mV_item.click();
                        sleep(ChatClient.overtime_1);
                        break;
                    }
                }
            }
        }
        String bank_no = null, name = null, money = null, status = null;
        while (true) {
            UiObject mTV_title = CodeUtils.getUiObject_textOrId("转账详情", "cn.com.nxy.mbank.hb:id/tvTopTextTitle");
            if (mTV_title.exists()) {
                showMsg("进入详情页");
                UiObject mV1 = CodeUtils.getUiObject_textOrClass("收款账号", "android.view.View");
                if (mV1.exists()) {
                    UiObject mET_bank_no = mV1.getFromParent(new UiSelector().className("android.widget.EditText").index(1));
                    if (mET_bank_no.exists()) {
                        bank_no = mET_bank_no.getText().replaceAll(" ", "");
                    }
                }
                UiObject mV2 = CodeUtils.getUiObject_textOrClass("收款户名", "android.view.View");
                if (mV2.exists()) {
                    UiObject mET_name = mV2.getFromParent(new UiSelector().className("android.widget.EditText").index(1));
                    if (mET_name.exists()) {
                        name = mET_name.getText().replaceAll(" ", "");
                    }
                }
                UiObject mV3 = CodeUtils.getUiObject_textOrClass("转账金额", "android.view.View");
                if (mV3.exists()) {
                    UiObject mET_money = mV3.getFromParent(new UiSelector().className("android.widget.EditText").index(1));
                    if (mET_money.exists()) {
                        money = mET_money.getText().replaceAll(" ", "");
                        if (!StringUtils.isEmpty(money)) {
                            money = Util.replaceMoney(money);
                        }
                    }
                }
                UiObject mV4 = CodeUtils.getUiObject_textOrClass("交易状态", "android.view.View");
                if (mV4.exists()) {
                    UiObject mET_status = mV4.getFromParent(new UiSelector().className("android.widget.EditText").index(1));
                    if (mET_status.exists()) {
                        status = mET_status.getText().replaceAll(" ", "");
                    }
                }
                String json = null;
                String mobileD = null;
                if (Double.parseDouble(list.get(0).getMoney()) == Double.parseDouble(money) && name.contains(list.get(0)
                        .getUserName()) && list.get(0).getBankNo().contains(bank_no) && Constants.getStr(status) == 0) {
                    json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':0,'type':1";
                    mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'兴业银行'," +
                            "'info':'交易成功'";
                } else {
                    String sumMoneyFile = user.getUserName() + BANK_FLAG + "_sumMoney.txt";
                    String oldMoney = FileHelper.readSDFile(sumMoneyFile);
                    switch (Constants.changeInAmount(BANK_FLAG, oldMoney, sumMoney, list.get(0).getMoney())) {
                        case "0":
                            json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':0,'type':1";
                            mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'湖北农信银行'," +
                                    "'info':'交易成功'";
                            break;
                        case "1":
                            json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':1,'type':1";
                            mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'湖北农信银行'," +
                                    "'info':'交易待处理...'";
                            break;
                        case "4":
                            json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':4,'type':1";
                            mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'湖北农信银行'," +
                                    "'info':'未查到条目'";
                            break;
                    }
                }
                failureToReport(json, mobileD);
                sleep(ChatClient.overtime_1);
                backTransferPage();
                break;
            }
        }
    }

    private void failureToReport(String json, String mobileD) {
        ChatClient.msg("<YZF>{'header':'" + ChatClient.S_CONFIRM_TRADE_RESULT_C + "'," + json + "," + mobileD + "}</YZF>");
        //删除处理过的订单
        mapDelete(list.get(0).getOrder());
        getmap();
        sleep(ChatClient.overtime_1);
    }

    /**
     * 关闭首页弹窗
     *
     * @throws UiObjectNotFoundException
     */
    private void closeRechargeDialog() throws UiObjectNotFoundException {
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_10, "警告弹窗超时...", Constants.notReport);
            if (isOutTime) {
                break;
            }
            UiObject mBTs = mDevice.findObject(new UiSelector().resourceId("cn.com.nxy.mbank.hb:id/ll_select_btn").index(3));
            if (mBTs.exists()) {
                UiObject mBT = mBTs.getChild(new UiSelector().resourceId("cn.com.nxy.mbank.hb:id/btn_cancel"));
                if (mBT.exists()) {
                    showMsg("关闭警告弹窗...");
                    mBT.click();
                    break;
                }
            }
        }
    }

    /**
     * 进入转账页
     */
    private void backTransferPage() throws UiObjectNotFoundException {
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_5, "未进入转账页...", Constants.notReport);
            if (isOutTime) {
                showMsg("返回上页");
                mDevice.pressBack();
                ChatClient.begintime = System.currentTimeMillis();//开始时间
            }
            UiObject mV = CodeUtils.getUiObject_textOrClass("账号转账", "android.view.View");
            if (mV.exists()) {
                mV.click();
                sleep(ChatClient.overtime_1);
            }
            UiObject mTV_title = CodeUtils.getUiObject_textOrId("账号转账", "cn.com.nxy.mbank.hb:id/tvTopTextTitle");
            if (mTV_title.exists()) {
                showMsg("以进入转账页");
                break;
            }
        }

    }

    public void showLog(String msg) {
        Log.i("湖北农信银行" + "==", "===《" + msg + "》===");
    }

    public void showMsg(String msg) {
        LogToFile.showLog("湖北农信银行", msg);
    }

    @Override
    public void keyPhone(int screenHeight) {

    }

    @Override
    public void keyPhone(String device) {

    }
}
